package net.tycmc.cty.ctyms.timejob.iems.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.configuration.ConfigurationException;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;
import net.tycmc.cty.ctyms.timejob.common.EquipVersionCommon;
import net.tycmc.cty.ctyms.timejob.iems.dao.OilOpenRateDAO;
import net.tycmc.cty.ctyms.timejob.iems.thread.OilOpenRunner;

public class OilOpenRateService {
	private OilOpenRateDAO oilOpenRateDAO;
	
	public void setOilOpenRateDAO(OilOpenRateDAO oilOpenRateDAO) {
		this.oilOpenRateDAO = oilOpenRateDAO;
	}


	/**
	 * 油门开度次数统计
	 **/
	public boolean statisticsOilOpenCounts(Map<String,Object> param) {
		//1、查询判断是否已经建立 油门开度的日增表，如果没有则建立
		String workTime = "";
		String isbakTable="";
		if(null !=param){
			workTime = param.get("workDay").toString().substring(0, 10);//2015-12-01
			oilOpenRateDAO.deleteOilOpen(workTime);//手动统计前先删除数据
		}else{
			workTime = DateUtil.addDay(-1);
		}
		try {
			isbakTable = DynamicReadConfig.getConfigProperty("config.properties", "OilOpenRate");
		} catch (ConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//为避免网站查询今天的数据报错，建立今天的表
		this.oilOpenRateDAO.checkCreateOilOpenRate(DateUtil.addDay(0),isbakTable);
		if(this.oilOpenRateDAO.checkCreateOilOpenRate(workTime,isbakTable)){
			String jppSoftVers = EquipVersionCommon.getSoftVerByAgrType("Vcl_JPP");
			//2、新增所有有油门开度信息的所有JPP设备的总条数
			if(this.oilOpenRateDAO.inertOilOpenAllCounts(workTime,jppSoftVers,isbakTable,param)){
				//3、查询PGN设备信息，
				List<Map<String,Object>> jppVclList = this.oilOpenRateDAO.queryPGNVclInfoList(workTime);
				System.out.println("油门开度获取设备总数量："+jppVclList.size());
				
				//4、新建线程池
				ExecutorService executorService = Executors.newFixedThreadPool(5);
				
				//5、遍历JPP设备，每20台设备进行分组，获取油门开度信息，并创建子线程
				StringBuffer vclIDS = new StringBuffer();
				String tempStr = "";
				for(int i=0;i<jppVclList.size();i++){
					Map<String,Object> oneVcl = jppVclList.get(i);
					if(i!=0 && (i%5==0 || i==(jppVclList.size()-1))){
						if(i==(jppVclList.size()-1)){
							vclIDS.append(MapGetter.getString(oneVcl,"VehicleID")+",");
						}
						tempStr = vclIDS.toString();
						//创建计算油门开度的子线程
						OilOpenRunner tempRun = new OilOpenRunner(tempStr.substring(0, tempStr.length()-1),workTime,isbakTable,param);
						
						executorService.submit(tempRun);
						
						vclIDS.setLength(0);
						vclIDS.append(MapGetter.getString(oneVcl,"VehicleID")+",");
					}else{
						vclIDS.append(MapGetter.getString(oneVcl,"VehicleID")+",");
					}
				}
			}else{
				
			}
		}else{
			
		}
		return true;
	}
	//获取前三天的统计数据
	public List<Map<String, Object>> getoilOpenCount() {
		List<String> list = new ArrayList<String>();
		list.add(DateUtil.addDay(-1));
		list.add(DateUtil.addDay(-2));
		list.add(DateUtil.addDay(-3));
		return oilOpenRateDAO.getOilOpenCount(list);
	}


	public int getExecuteCount(String isExectue) {
		return oilOpenRateDAO.getExeOilOpenCount(isExectue);
	}

}
